﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hotel_Admin.Common;
using HOTEL.Dao;
using Hotel.Util;
using Hotel.Domain;
using System.Drawing;
using Hotel_Admin.Web.Common;
using Microsoft.Reporting.WebForms;

namespace Hotel_Admin
{
    public partial class KH_TraPhong : Authentication
    {
        private PhieuDatPhongDao phieudatphongDao = new PhieuDatPhongDao();
        protected void Page_Load(object sender, EventArgs e)
        {

            CheckViewPermission();
            if (!Page.IsPostBack)
            {
                LoadReference();
                BindDataForGird();
            }
        }
        protected void LoadReference()
        {
            
        }
        protected bool IsValidData()
        {
            
            return true;
        }
        protected void BindDataForGird()
        {
            var list = phieudatphongDao.SearchForTraPhong(txtCMND.Text.Trim(), txtTenKhachHang.Text.Trim(), null, null, TRANGTHAI_PHIEUDATPHONG.DANGDAT.GetHashCode());
            gvList.DataSource = list;
            gvList.DataBind();
            upGird.Update();
        }
        protected void ClearForm()
        {
            txtCMND.Text = "";
            txtTenKhachHang.Text = "";
            upInfor.Update();
        }
        protected void CheckViewPermission()
        {
            if (!HasViewPermission(Pages.KH_TraPhong.GetHashCode()))
            {
                Redirect(WebURL.UrlWebDinie);
            }
        }
        
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var require = e.CommandName;
            var id = e.CommandArgument.ToString();
            switch (require)
            {
                case "CheckOut":
                    var phieudat = phieudatphongDao.Get(id);
                    if (phieudat != null)
                    {
                        txtTenKhachHang.Text = phieudat.HoTen;
                        txtMST.Text = "";
                        txtDiaChi.Text = "";
                        upInfor.Update();
                        phieudatChose = phieudat;
                    }
                    
                    ShowInfor("Thanh toán thành công");
                    break;
            }
        }
        protected PHIEU_DAT_PHONG phieudatChose
        {
            get
            {
                return Session[SessionKey.SessionCurrernPhieuDat] as PHIEU_DAT_PHONG;
            }
            set
            {
                Session[SessionKey.SessionCurrernPhieuDat] = value;
            }
        }
        protected HOA_DON HoaDon
        {
            get
            {
                return Session["Hoadon"] as HOA_DON;
            }
            set
            {
                Session["Hoadon"] = value;
            }
        }
        protected void ShowReport()
        {
            var myds = new ReportClasscs().Rp_HoaDon(HoaDon.MaHoaDon);
            ReportDataSource ds = new ReportDataSource("HoaDon", myds.Tables[0]);
            rpViewer.LocalReport.DataSources.Clear();
            rpViewer.LocalReport.ReportPath = "ReportViews/Hoadon.rdlc";
            rpViewer.LocalReport.DataSources.Add(ds);
            ReportParameter rp = new ReportParameter("rpTenKhachHang",HoaDon.TenKhachHang);
            ReportParameter rp2 = new ReportParameter("rpDiaChi", HoaDon.DiaChi);
            ReportParameter rp3 = new ReportParameter("rpMST", HoaDon.MaSoThue);
            rpViewer.LocalReport.SetParameters(new ReportParameter[] { rp, rp2, rp3});
            rpViewer.LocalReport.Refresh();
            divCR.Visible = true;
            upnlReport.Update();
            CloseWaitingDialog();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!HasInsertPermission(Pages.KH_TraPhong.GetHashCode()))
            {
                Redirect(WebURL.UrlWebDinie);
            }
            if (String.IsNullOrEmpty(txtDiaChi.Text.Trim()))
            {
                ShowError("Địa chỉ không được trống",txtDiaChi.ClientID);
                return;
            }
            HOA_DON objhd = new HOA_DON();
            var msg = phieudatphongDao.TinhTien(ref objhd,phieudatChose, txtTenKhachHang.Text, txtDiaChi.Text.Trim(), txtMST.Text.Trim(), UserLogin.MaNV, phieudatChose.NgayDen.Value,DateTime.Now);
            if (msg.MsgType != MessageType.Info)
            {
                ShowError("Thanh toán không thành công");
                CloseWaitingDialog();
                return;
            }
            else
            {
                // bind report
                HoaDon = objhd;
                ShowReport();
                ShowInfor("Thanh toán thành công");
                BindDataForGird();
                
                CloseWaitingDialog();
                return;
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            ClearForm();
            CloseWaitingDialog();
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            BindDataForGird();
            CloseWaitingDialog();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)   
        {
            if (!e.Row.RowType.Equals(DataControlRowType.DataRow)) return;
            var list = gvList.DataSource as List<PHIEU_DAT_PHONG>;
            var panel = e.Row.FindControl("status") as Panel;
            if (panel != null)
            {
                var item = list[e.Row.RowIndex + gvList.PageIndex * gvList.PageSize];
                panel.BackColor = Color.FromName(item.TRANG_THAI_PHIEU_DAT.Color);
            }
        }

    }
}